package com.com.yunzong.rainbowbridge;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.com.yunzong.rainbowbridge.network.NetWorkUtil;
import com.com.yunzong.rainbowbridge.network.Request_CallBack;
import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.network.ApiCallback;

import java.util.List;


public class CallBackService extends Service {
    String orderId;
    int firstChannel;
    int broadcastResult;
    String  reason;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d("zx", "---------onStartCommand----->onStartCommand");
        if(intent!=null){
            try{

               List<Request_CallBack> request_callBacks= (List<Request_CallBack>) intent.getSerializableExtra("speechCallbackList");
                doBatchCallBack(request_callBacks);
            }catch (Exception e){
                e.printStackTrace();
//                LogBackUtil.e(e.toString());
            }


        }
       return super.onStartCommand(intent, flags, startId);

    }


    private static void  doBatchCallBack(List<Request_CallBack> request_callBacks){
        NetWorkUtil.doVoiceBatchCallBack(request_callBacks, new ApiCallback() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {

            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {

            }
        });
    }

    /**
     * 客户端收到语音推送后回调服务端播报结果
     * @param orderId
     * @param guid
     * @param networkType
     * @param firstChannel
     * @param reason
     * @param broadcastResult
     */
    private static void callBack(String orderId, String guid, String networkType,
                                 int firstChannel, String reason, int broadcastResult,long receiveTime,long broadcastTime) {

//        orderId=intent.getStringExtra("orderId");
//        firstChannel=intent.getIntExtra("firstChannel",-1);
//        broadcastResult=intent.getIntExtra("broadcastResult", -1);
//        reason=intent.getStringExtra("reason");
//        long receiveTime = intent.getLongExtra("receiveTime", 0l);
//        long broadcastTime = intent.getLongExtra("broadcastTime", 0l);
//
//        String guid= PreferencesUtils.getString(this,BaseConfigs.STRING_GUID);
//
//        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//        String networkType="";
//        if (networkInfo != null && networkInfo.isConnected()) {
//            networkType = networkInfo.getTypeName();
//        }

        Log.d("zx", "---------callBack----->onCompleted");
        Request_CallBack request_callBack=new Request_CallBack();
        request_callBack.setOrderId(orderId);
        request_callBack.setGuid(guid);
        request_callBack.setNetworkType(networkType);
        request_callBack.setFirstChannel(firstChannel);
        request_callBack.setReason(reason);
        request_callBack.setAchievedTime(receiveTime+"");
        request_callBack.setBroadcastTime(broadcastTime+"");
        request_callBack.setBroadcastResult(broadcastResult);
        request_callBack.setPlatform(BaseConfigs.PLATFORM.ANDROID);
        NetWorkUtil.doVoiceCallBack(request_callBack, new ApiCallback() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {

            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {

            }
        });
    }

}
